package com.mao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mao.common.Result;
import com.mao.dto.DishDto;
import com.mao.entity.Category;
import com.mao.entity.Dish;
import com.mao.entity.DishFlavor;
import com.mao.service.CategoryService;
import com.mao.service.DishFlavorService;
import com.mao.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 菜品管理
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 菜品新增
     * @param dishDto
     * @return
     */
    @PostMapping
    public Result<String> saveWithFlavor(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return Result.success("菜品保存成功");
    }

    /**
     * 菜品分页信息
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name){
        Page<DishDto> dishDtoPage = dishService.page(page, pageSize, name);
        return Result.success(dishDtoPage);
    }

    /**
     * 查询单个菜品基本信息及口味
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishDto> get(@PathVariable Long id){
        DishDto dishAndFlavor = dishService.getWithFlavor(id);
        return Result.success(dishAndFlavor);
    }

    /**
     * 更新菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return Result.success("菜品信息更新成功");
    }


    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam("ids") List<Long> ids){
        dishService.deleteById(ids);
        return Result.success("删除菜品成功");
    }

    /**
     * 修改菜品的售卖状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> status(@PathVariable("status")Integer status,@RequestParam("ids") List<Long> ids){
        dishService.status(status,ids);
        return Result.success("售卖状态已修改");
    }

    /**
     * 查询所有菜品
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public Result<List<DishDto>> list(Dish dish){
        List<DishDto> dishDtoList = dishService.get_list(dish);
        return Result.success(dishDtoList);
    }
}
